﻿package Classes.Score{

	import Classes.Users.*;
	import com.hurlant.crypto.*;
	import com.hurlant.crypto.hash.*;
	import flash.utils.ByteArray;
	import com.hurlant.util.Hex;

	public final class Keeper {

		public var score:Number=0;
		public var mdHash:String=calcHash(0);

		private static var instance:Keeper = new Keeper();

		public function Keeper() {
			if (instance) {
				throw new Error("Singleton and can only be accessed through Singleton.getInstance()");
			}
		}

		public static function getInstance():Keeper {
			return instance;
		}

		public function addScore(n:Number):void {
			
			if (mdHash == calcHash(this.score)){

				this.score+=n;
				mdHash = calcHash(this.score);
				
			}
			
		}

		public function removeScore(n:Number):void {

			if (mdHash == calcHash(this.score)){

				this.score-=n;
				mdHash = calcHash(this.score);
				
			}

		}

		public function getScore():Number {

			if (mdHash == calcHash(this.score)){

				return this.score;
				
			}else{
				
				return -1000;
				
			}

		}

		public function resetScore():void {

			if (mdHash == calcHash(this.score)){

				this.score=0;
				mdHash = calcHash(this.score);
				
			}

		}
		
		public function setCheatScore():void {

			if (mdHash == calcHash(this.score)){

				this.score=-10000;
				mdHash = calcHash(this.score);
				
			}

		}
		
		public function calcHash(s):String{
			
			s = Crypto.getHash("md5").hash(Hex.toArray(Hex.fromString(s+"[(!#@-+")));
			s = Crypto.getHash("md5").hash(Hex.toArray(Hex.fromString("[(!#@-48hkd1^+"+s)));
			return s;
			
		}

	}

}